<?php

namespace Procesos\Cargador\Workflow;

use Entidades\Workflow\Documento\PropiedadesCampo;

use Entidades\Workflow\Documento\DocumentoTipo,
	Entidades\Workflow\Documento\CampoDocumento,
	Entidades\Workflow\Documento\VarianteDocumento;
/*
 * Created on 15/05/2006
 *
 * @author Edgar Chacon
 */

class Relacion {
	private $idRol;
	private $idLugar;
	private $idCampo;
	private $view;
	private $mod;
	
	public function __construct($idCampo, $idRol, $idLugar, $mod, $view ) {
		$this->idCampo = $idCampo;
		$this->idLugar = $idLugar;
		$this->idRol = $idRol;
		$this->mod = $mod;
		$this->view =$view;
	}
	public function getIdRol(){
		return $this->idRol;
	}
	
	public function getIdLugar() {
		return $this->idLugar;
	}
	
	public function getIdCampo() {
		return $this->idCampo;
	}
	
	public function getIdMod() {
		return $this->mod;
	}

	public function getIdView() {
		return $this->view;
	}
	
}

class DoctypeHandler extends BaseHandler {

	/**
	 * @type MDocparameter
	 */
	private $currDocParameter;

	// ----------------------------------------
	// Props
	// ----------------------------------------
	/**
	 * @type DocumentoTipo
	 */
	private $docTipo;
	
	private $estado = 0;
	
	private $campoActual;
	
	/**
	 * 
	 * @type array
	 */
	private $propiedades = array();

	/**
	 * @type array
	 */
	private $camposPorNombre = array ();
	
	/**
	 * @type array
	 */
	private $variantesPorNombre = array ();
	
	/**
	 * @type boolean
	 */
	private $validez;
	
	// ----------------------------------------

	public function __construct() {
		// Empty
	}

	// ----------------------------------------
	// Handler Methods
	// ----------------------------------------

	private function docTipoBeg($parser, $tag, $attributes) {
		$this->docTipo = new DocumentoTipo();
		$this->docTipo->setIdentificador($attributes['idDoc']);
		$this->estado = 0;
		$this->validez = true;
	}

	private function campoDocumentoBeg($parser, $tag, $attributes){
		$this->campoActual = new CampoDocumento();
		$this->campoActual->setIdCampo($attributes['idField']);
		$this->campoActual->setIdTipoDeCampo($attributes['fieldType']);
		$this->estado = 1; 
	}
	
	private function propertyBeg($parser, $tag, $attributes) {
		$idCampo = $this->campoActual->getIdCampo();
		$idRol = $attributes['role'];
		$idLugar = $attributes['place'];
		$mod = $attributes['mod'];
		$view = $attributes['view'];
		$this->propiedades[] = new Relacion($idCampo, $idRol, $idLugar, $mod, $view);
	}
	
	private function nameEnd($parser, $tag) {
		if($this->estado == 0) $this->docTipo->setNombre($this->text);
		if($this->estado == 1) $this->campoActual->setNombreCampo($this->text);
	}

	private function urlEditionEnd($parser, $tag) {
		$this->docTipo->setDireccionEdicion($this->text);
	}

	private function urlViewEnd($parser, $tag) {
		$this->docTipo->setDireccionVisualizacion($this->text);
	}

	private function urlForwardEnd($parser, $tag) {
		$this->docTipo->setDireccionAvance($this->text);
	}
	
	private function docFieldEnd($parser, $tag) {		
		if (isset ($this->camposPorNombre[$this->campoActual->getIdCampo()])) {
			throw new Exception("Duplicated field " + $this->campoActual->getNombreCampo());
			exit ();
		}
		$this->camposPorNombre[$this->campoActual->getIdCampo()] = $this->campoActual;
		$this->docTipo->setCampos($this->campoActual);

	}
	
	private function variantEnd($parser, $tag) {
		$variante = new VarianteDocumento();
		$variante->setNombre($this->text);
		if (isset ($this->variantesPorNombre[$variante->getNombre()])) {
			throw new Exception("Duplicated variant " + variant . getName());
			exit ();
		}
		$this->docTipo->setVariantes($variante);
		$this->variantesPorNombre[$variante->getNombre()] = $variante;
	}
	
	private function documentEnd($parser, $tag) {
		foreach ($this->camposPorNombre as $clave => $campo){
			$salida = "clave ".$clave. " valor ". $campo->getNombreCampo()." tipo de campo ".$campo->getIdTipoDeCampo() . "\n";
			print $salida;
		}
		foreach ($this->propiedades as $propiedadCampo){
			$salida = "--------------" . $propiedadCampo->getIdCampo() ." " . $propiedadCampo->getIdRol() . "\n ";
			print $salida;
		}
	
	}

	// ----------------------------------------
	// BaseHandler Methods
	// ----------------------------------------
	public function startElement($parser, $tag, $attributes) {
		parent :: startElement($parser, $tag, $attributes);

		switch ($tag) {
			case 'document' :
				$this->docTipoBeg($parser, $tag, $attributes);
				break;
			case 'docField' :
				$this->campoDocumentoBeg($parser, $tag, $attributes);
				break;
			case 'property' :
				$this->propertyBeg($parser, $tag, $attributes);
				break;
		}
	}

	public function endElement($parser, $tag) {
		parent :: endElement($parser, $tag);
		switch ($tag) {
			case 'name' :
				$this->nameEnd($parser, $tag);
				break;
			case 'urlEdition' :
				$this->urlEditionEnd($parser, $tag);
				break;
			case 'urlView' :
				$this->urlViewEnd($parser, $tag);
				break;
			case 'urlForward' :
				$this->urlForwardEnd($parser, $tag);
				break;
			case 'docField' :
				$this->docFieldEnd($parser, $tag);
				break;
			case 'variant' :
				$this->variantEnd($parser, $tag);
				break;
			case 'document' :
				$this->documentEnd($parser, $tag);
				break;
		}
	}

	// ----------------------------------------
	// Props Methods
	// ----------------------------------------
	/**
	 * @return MDocType
	 */
	public function getDocTipo() {
		return $this->docTipo;
	}

	/**
	 * @return array
	 */
	public function getCampoPorNombre() {
		return $this->camposPorNombre;
	}
	
	/**
	 * @return array
	 */
	public function getVariantByName() {
		return $this->variantByName;
	}
}
?>
